var express = require('express');
var router = express.Router();


var mongoose = require('mongoose'); //引入对象
var TodoModel = mongoose.model('user');//引入模型
var URL = require('url'); //引入URL中间件，获取req中的参数需要

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('login');
});

router.get('/index',function(req,res,next){
   res.render('index');
});

router.get('/ob1', function(req, res, next) {
  res.render('ob1');
});

router.get('/ob2', function(req, res, next) {
  res.render('ob2');
});

router.get('/ob3', function(req, res, next) {
  res.render('ob3');
});

router.get('/ob4', function(req, res, next) {
  res.render('ob4');
});

router.get('/ob5', function(req, res, next) {
  res.render('ob5');
});

router.get('/ob6', function(req, res, next) {
  res.render('ob6');
});

router.get('/data_CO', function(req, res, next) {
  res.render('data_CO');
});

router.get('/data_SO2', function(req, res, next) {
  res.render('data_SO2');
});

router.get('/data_O3', function(req, res, next) {
  res.render('data_O3');
});

router.get('/data_PM10', function(req, res, next) {
  res.render('data_PM10');
});

router.get('/data_PM25', function(req, res, next) {
  res.render('data_PM25');
});

router.get('/history', function(req, res, next) {
  res.render('history');
});

router.get('/list', function(req, res, next) {
  res.render('list');
});

router.get('/standard', function(req, res, next) {
  res.render('standard');
});

router.get('/help', function(req, res, next) {
  res.render('help');
});

router.get('/home',function(req,res,next){
    res.render('home');
})

router.get('/api',function(req,res,next){
    res.render('api');
})

router.get('/test',function(req,res,next){
    res.render('test');
})

router.get('/search', function(req, res, next) {
  var date = [];
  var myDate = new Date();
  var y = myDate.getFullYear();
  var station = ["彩香","上方山(对照点)","轧钢厂","相城区","吴中区","木渎中学"];
  //var m = (myDate.getMonth()+1);
  //var d =  myDate.getDate();
  //var h =  myDate.getHours()+1;

  var m = 1;
  var d = 13;
  var h = 3;
  //2021-01-11T08:00:00Z
  for(let i=0;i<12;i++){
     var time = "";
     h = h-1;
     if(h<0){
        h = 23;
        d = d -1;
        if(d<0){
            d = 31;
            m = m -1;
        }
     }
     var t_m = m;
     var t_d = d;
     var t_h = h;
     if(t_m<10) t_m = '0' + t_m;
     if(t_d<10) t_d = '0' + t_d;
     if(t_h<10) t_h = '0' + t_h;
     time = y+"-"+t_m+"-"+t_d+"T"+t_h+":00:00Z";
     date[i]=time;
  }
  date.reverse();
  //var condition="({\"position_name\":\"彩香\",$or:[";
  var condition = "({$or:[";
  for(let i=0;i<date.length;i++){
        for(let j=0;j<6;j++){
            var tmp = "{\"position_name\":"+"\""+station[j]+"\""+","+"\"time_point\":"+"\""+date[i]+"\"}";
            condition+=tmp;
            if(j!=5 || i!=date.length-1) condition+=",";
        }
  }
  condition +="]})";
  var cond = eval("("+condition+")");
  var query =TodoModel.find(cond);
  query.exec(function(err, aa) { res.send(aa);});

});

router.get('/history_data', function(req, res, next) {
  var date = ['2021-01-12T13:00:00Z',
                '2021-01-12T14:00:00Z',
                '2021-01-12T15:00:00Z',
                '2021-01-12T16:00:00Z',
                '2021-01-12T17:00:00Z',
                '2021-01-13T13:00:00Z',
                '2021-01-13T14:00:00Z',
                '2021-01-13T15:00:00Z',
                '2021-01-13T16:00:00Z',
                '2021-01-13T17:00:00Z',
                '2021-01-14T13:00:00Z',
                '2021-01-14T14:00:00Z',
                '2021-01-14T15:00:00Z',
                '2021-01-14T16:00:00Z',
                '2021-01-14T17:00:00Z'
              ];
  var station = ["彩香","上方山(对照点)","轧钢厂","相城区","吴中区","木渎中学"];
  var condition = "({$or:[";
  for(let i=0;i<date.length;i++){
        for(let j=0;j<6;j++){
            var tmp = "{\"position_name\":"+"\""+station[j]+"\""+","+"\"time_point\":"+"\""+date[i]+"\"}";
            condition+=tmp;
            if(j!=5 || i!=date.length-1) condition+=",";
        }
  }
  condition +="]})";
  var cond = eval("("+condition+")");
  //console.log(condition);
  var query =TodoModel.find(cond);
  query.exec(function(err, aa) { res.send(aa);});

});

module.exports = router;
